'''
# ---以下以子图形式------------------------------------
# 定义一个格式化函数，用于将数值转换为带有千位分隔符的字符串
def format_x(x, pos):
    return f"${x:,.0f}"

# 读取CSV文件，第一行作为列名
df = pd.read_csv('sensitivity_table-副本.csv', header=0)

# 选择1列和2列作为x和y轴的自变量
x = df['Unit CLAP/day\n[$]']
y = df['Unit PLAC/day\n[$]']

# 选择2、21、25、26列作为因变量
z1 = df.iloc[:, 2]  # 第2列:Ratio of unit CLAP to unit PLAC
z2 = df.iloc[:, 3]  # 第3列:Aver. TAC of base schedule[$]
z3 = df.iloc[:, 22]  # 第22列:Aver. suggested postponement[day]
z4 = df.iloc[:, 23]  # 第23列:Aver. reduced TAC[$]
z5 = df.iloc[:, 24]  # 第24列:Aver. CLAP[$]
z6 = df.iloc[:, 26]  # 第26列:Aver. PLAC[$]
z = [z1, z2, z3, z4, z5, z6]
legend_handles = []

# 设置图形的大小
plt.figure(figsize=(15, 10))  # 调整图形大小以适应3个子图

# 创建一个1行3列的subplot网格
fig, axs = plt.subplots(1, 3, figsize=(15, 5))  # 1行3列

# 循环遍历每个子图，绘制数据
for i, ax in enumerate(axs):
    for line_no in z:
        # 设置子图标题
        ax.set_title(f"Subplot {i+1}")
        # 绘制数据
        y1 = y[i * 3: i * 3 + 3]
        z1 = z[line_no][i * 3: i * 3 + 3]
        line, = ax.plot(y1, z1, marker='^', linestyle='-', color='red',
                         label=str(line_no + 1) + "." + legend_labels[0])

        legend_handles.append(line)
        ax_twin = ax.twinx()
        z2 = z[line_no][i * 3: i * 3 + 3]
        line, = ax_twin.plot(y1, z2, marker='v', linestyle='-.', color='orange',
                         label=str(line_no) + "." + legend_labels[1])
        line_no += 1
        legend_handles.append(line)

        # -------------
        # Plot 'Avg. suggested postponement[day]'
        line, = ax.plot(y[i * 3: i * 3 + 3], z[i][i * 3: i * 3 + 3], marker='s', linestyle=(0, (1, 10)), color='black',
                         label=str(line_no) + "." + legend_labels[2])
        line_no += 1
        legend_handles.append(line)

        # Plot 'Avg. reduced TAC[$]'
        line, = ax2.plot(y[i * 3: i * 3 + 3], z4[i * 3: i * 3 + 3], marker='d', linestyle=(0, (3, 10, 1, 10)),
                         color='green',
                         label=str(line_no) + "." + legend_labels[3])
        line_no += 1
        legend_handles.append(line)
        # Plot 'Avg. CLAP[$]'
        line, = ax2.plot(y[i * 3: i * 3 + 3], z5[i * 3: i * 3 + 3], marker='o', linestyle='--', color='blue',
                         label=str(line_no) + "." + legend_labels[4])
        line_no += 1
        legend_handles.append(line)

        # Plot 'Avg. PLAC[$]'
        line, = ax2.plot(y[i * 3: i * 3 + 3], z6[i * 3: i * 3 + 3], marker='+', linestyle=':', color='purple',
                         label=str(line_no) + "." + legend_labels[5])
        line_no += 1
        legend_handles.append(line)

        # Add a legend to the plot
        ax.legend(handles=legend_handles, loc='best')

        # Adjust the spacing between subplots
        plt.tight_layout()

        # Set global x and y axis labels
        fig.text(0.50, 0.008, 'Unit PLAC/day [$]', ha='center')

        # Create a FuncFormatter object and pass the formatting function
        formatter = FuncFormatter(format_x)

        # Apply the formatter to the x and y axis tick labels
        plt.gca().xaxis.set_major_formatter(formatter)
        plt.gca().yaxis.set_major_formatter(formatter)

        # ---------------


        # # ax.plot(y[0:3], z1[0:3], marker='^', linestyle='-', color='red', label='Ratio of unit CLAP to unit PLAC')
        # ax.plot(y[0:3], z2[0:3], marker='v', linestyle='-.', color='orange', label='Avg. TAC of base schedule[$]')
        # ax.plot(y[0:3], z3[0:3], marker='s', linestyle=(0, (1, 10)), color='black', label='Avg. suggested postponement[day]')
        #
        # # 创建第二个轴，共享x轴，用于右侧y轴
        # ax2 = ax.twinx()
        # ax2.plot(y[0:3], z4[0:3], marker='d', linestyle=(0, (3, 10, 1, 10)), color='green', label='Avg. reduced TAC[$]')
        # ax2.plot(y[0:3], z5[0:3], marker='o', linestyle='--', color='blue', label='Avg. CLAP[$]')
        # ax2.plot(y[0:3], z6[0:3], marker='+', linestyle=':', color='purple', label='Avg. PLAC[$]')
        # ax2.set_ylabel('Additional cost[$]')
        # 添加图例
        ax.legend(loc='upper left')
        ax2.legend(loc='upper right')

# 调整子图间距
plt.tight_layout()

# 显示图形
plt.show()

'''
'''
# Subplots
fig, axs = plt.subplots(1, 3, sharex=True, sharey=True)
# plt.ylabel("Ratio of unit PLAC to unit CLAP\nAvg. suggested postponement[day]")


for i in range(2, -1, -1):
    line_no = 1
    # Initialize legend handles
    legend_handles = []
    # Set the figure size
    plt.figure(figsize=(15, 5))
    # Create a 1x1 subplot grid with shared x and y axes
    # fig, axs = plt.subplots(1, 1, sharex=True, sharey=True)

    plt.xticks(y[0:3])

    CLAP_str = format_x(df.iloc[i * 3, 0], 3)
    axs[i].set_title("Unit CLAP/day=" + CLAP_str)
    axs[i].set_ylabel("Ratio of unit PLAC to unit CLAP\nAvg. suggested postponement[day]")
    ax2 = axs[i].twinx()
    ax2.set_ylabel("Additional cost[$]")
    # Plot 'Ratio of unit CLAP to unit PLAC'
    line, = axs[i].plot(y[i * 3: i * 3 + 3], z1[i * 3: i * 3 + 3], marker='^', linestyle='-', color='red',
                     label=str(line_no) + "." + legend_labels[0])
    line_no += 1
    legend_handles.append(line)
    # Plot 'Avg. TAC of base schedule[$]'
    line, = ax2.plot(y[i * 3: i * 3 + 3], z2[i * 3: i * 3 + 3], marker='v', linestyle='-.', color='orange',
                     label=str(line_no) + "." + legend_labels[1])
    line_no += 1
    legend_handles.append(line)
    # Plot 'Avg. suggested postponement[day]'
    line, = axs[i].plot(y[i * 3: i * 3 + 3], z3[i * 3: i * 3 + 3], marker='s', linestyle=(0, (1, 10)), color='black',
                     label=str(line_no) + "." + legend_labels[2])
    line_no += 1
    legend_handles.append(line)
    # Plot 'Avg. reduced TAC[$]'
    line, = ax2.plot(y[i * 3: i * 3 + 3], z4[i * 3: i * 3 + 3], marker='d', linestyle=(0, (3, 10, 1, 10)),
                     color='green',
                     label=str(line_no) + "." + legend_labels[3])
    line_no += 1
    legend_handles.append(line)
    # Plot 'Avg. CLAP[$]'
    line, = ax2.plot(y[i * 3: i * 3 + 3], z5[i * 3: i * 3 + 3], marker='o', linestyle='--', color='blue',
                     label=str(line_no) + "." + legend_labels[4])
    line_no += 1
    legend_handles.append(line)

    # Plot 'Avg. PLAC[$]'
    line, = ax2.plot(y[i * 3: i * 3 + 3], z6[i * 3: i * 3 + 3], marker='+', linestyle=':', color='purple',
                     label=str(line_no) + "." + legend_labels[5])
    line_no += 1
    legend_handles.append(line)

    # Add a legend to the plot
    axs[i].legend(handles=legend_handles, loc='best')

    # Adjust the spacing between subplots
    plt.tight_layout()

    # Set global x and y axis labels
    fig.text(0.50, 0.008, 'Unit PLAC/day [$]', ha='center')

    # Create a FuncFormatter object and pass the formatting function
    formatter = FuncFormatter(format_x)

    # Apply the formatter to the x and y axis tick labels
    plt.gca().xaxis.set_major_formatter(formatter)
    plt.gca().yaxis.set_major_formatter(formatter)

    # Save the plot with a filename that includes the value of CLAP_str
    plt.savefig(f"sensitivity_analysis_CLAP{CLAP_str}")

    # Display the plot on the screen
    plt.show()
'''